# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=dcmtk
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=3.6.7
pkgrel=5
pkgdesc="A collection of libraries and applications implementing large parts of the DICOM standard (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url='http://dicom.offis.de/dcmtk'
license=('spdx:BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-icu"
         "${MINGW_PACKAGE_PREFIX}-libiconv"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libsndfile"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-openjpeg2"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-autotools")
source=("https://dicom.offis.de/download/dcmtk/dcmtk${pkgver//./}/${_realname}-${pkgver}.tar.gz"
        "001-fix-cmake-config-install-destination.patch"
        "002-properly-detect-canonical-host.patch")
sha256sums=('7c58298e3e8d60232ee6fc8408cfadd14463cc11a3c4ca4c59af5988c7e9710a'
            'ff124b6f26c5a5982268597c6285e2e320a248a090046b45618e79cb8a6fc099'
            'b9cd641267f9aeb055608f045cc91ad5b99b071897aa849ed3282aa03b5ae925')

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}
  patch -p1 -i "${srcdir}"/001-fix-cmake-config-install-destination.patch
  patch -p1 -i "${srcdir}"/002-properly-detect-canonical-host.patch

  cd config
  ./rootconf
}

build() {
  # this is just to get arith.h
  cd "${srcdir}"/${_realname}-${pkgver}

  ./configure --ignore-deprecation
  make config-tests-all
  install -D config/include/dcmtk/config/arith.h "${srcdir}"/build-${MSYSTEM}/config/include/dcmtk/config/arith.h

  # continue the build using cmake
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -DDCMTK_WITH_ICONV=ON \
      -DDCMTK_WITH_ICU=ON \
      -DDCMTK_WITH_LIBPNG=ON \
      -DDCMTK_WITH_LIBXML=ON \
      -DDCMTK_WITH_OPENJPEG=ON \
      -DDCMTK_WITH_OPENSSL=ON \
      -DDCMTK_WITH_SNDFILE=ON \
      -DDCMTK_WITH_TIFF=ON \
      -DDCMTK_WITH_ZLIB=ON \
      ../${_realname}-${pkgver}

  "${MINGW_PREFIX}"/bin/cmake.exe --build .
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"

  "${MINGW_PREFIX}"/bin/cmake.exe --build . --target test
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  # Fix pkgconfig file
  sed -e "s|${PREFIX_WIN}|\"\$\{prefix\}\"|g" -i "${pkgdir}"${MINGW_PREFIX}/lib/pkgconfig/dcmtk.pc
  # Fix cmake-config files
  find "${pkgdir}${MINGW_PREFIX}/lib/cmake" -type f -name '*.cmake' \
    -exec sed -i -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" {} \;

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYRIGHT" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
